﻿// Math Parser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EquationSolver
{
	class MathParser
	{

		public MathParser()
		{

		}

		public double EvalExpression( char[] expr )
		{
			return parseSummands( expr, 0 );
		}

		private double parseSummands( char[] expr, int index )
		{
			double x = parseFactors( expr, ref index );
			while (true)
			{
				char op = expr[index];
				if (op != '+' && op != '-')
					return x;
				index++;
				double y = parseFactors( expr, ref index );
				if (op == '+')
					x += y;
				else
					x -= y;
			}
		}

		private double parseFactors( char[] expr, ref int index )
		{
			double x = GetDouble( expr, ref index );
			while (true)
			{
				char op = expr[index];
				if (op != '/' && op != '*')
					return x;
				index++;
				double y = GetDouble( expr, ref index );
				if (op == '/')
				{
					x /= y;
				} else
				{
					x *= y;
				}
			}
		}

		private double GetDouble( char[] expr, ref int index )
		{
			string dbl = "";
			while (((int)expr[index] >= 48 && (int)expr[index] <= 57) || expr[index] == 46)
			{
				dbl = dbl + expr[index].ToString();
				index++;
				if (index == expr.Length)
				{
					index--;
					break;
				}
			}
			return double.Parse( dbl );
		}
	}
}
